package com.leilei;

import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;

import javax.servlet.http.HttpServletRequest;

/**
 * @author: lei
 * @date: 2024-07-29 22:01
 * @desc: 加解密自动装配类
 */
@EnableConfigurationProperties(SecretProperties.class)
public class SecretAutoConfiguration {

    private final SecretProperties secretProperties;

    public SecretAutoConfiguration(SecretProperties secretProperties) {
        this.secretProperties = secretProperties;
    }

    /**
     * 开启请求加密则启用
     * @author lei
     * @date 2024-07-30 09:46:17
     * @param
     * @return WebConfiguration
     */
    @Bean
    @ConditionalOnProperty(prefix = SecretProperties.PREFIX, name = "req", havingValue = "true",matchIfMissing = true)
    public WebConfiguration webConfig() {
        return new WebConfiguration(secretProperties);
    }

    /**
     * 只要请求加密启用则响应加密就启用，或者默认请求开启，响应关闭则不启用
     * @author lei
     * @date 2024-07-30 09:46:39
     * @param httpServletRequest
     * @return EncryptResponseBodyAdvice
     */
    @Bean
    @ConditionalOnProperty(prefix = SecretProperties.PREFIX, name = {"req","resp"}, havingValue = "true",matchIfMissing = true)
    public EncryptResponseBodyAdvice encryptResponseBodyAdvice(HttpServletRequest httpServletRequest) {
        return new EncryptResponseBodyAdvice(httpServletRequest);
    }
}
